package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by karkarrotxu on 2017/8/1.
 */
public final class ApiVodUtil {
    private static Logger logger = LoggerFactory.getLogger(ApiVodUtil.class);

    public static final int ANALYZE_RESULT_ERROR = -999;
    public static final int ANALYZE_RESULT_RESPONSE_FAIL = -100;
    public static final int ANALYZE_RESULT_DECODE_ERROR = -2;
    public static final int ANALYZE_RESULT_NO_DATA = -1;
    public static final int ANALYZE_RESULT_SUC = 200;


    private ApiVodUtil() {
    }

    public static final String VOD_SERVER_URL = Configuration.getString("vod.server.url");

    public static JSONObject doStartRecord( JSONObject jsonObject ) {
        JSONObject sendObj = buildPublicParams(jsonObject);
        sendObj.put("func", "enable");
        return  modifyChannel(sendObj);
    }

    private static JSONObject modifyChannel(JSONObject jsonObject) {
        jsonObject.put("username", "epg_system");
        jsonObject.put("from_client", true);
        return ApiHttpRequestUtil.postMethod(VOD_SERVER_URL + "v1/channel/modify", null, jsonObject, 4000);
    }

    public static JSONObject doStopRecord(JSONObject jsonObject) {
        JSONObject sendObj = buildPublicParams(jsonObject);
        sendObj.put("func", "disable");
        return modifyChannel(sendObj);
    }

    private static JSONObject buildPublicParams(JSONObject jsonObject) {
        JSONObject retValue = new JSONObject();
        retValue.put("src_channel_ip", jsonObject.get("channel_ip"));
        retValue.put("channel_ip", jsonObject.get("channel_ip"));
        retValue.put("src_channel_addr", jsonObject.get("channel_addr"));
        retValue.put("channel_addr", jsonObject.get("channel_addr"));
        retValue.put("channel_name", jsonObject.get("channel_name"));
        retValue.put("channel_code", jsonObject.get("channel_code"));
        retValue.put("src_channel_code", jsonObject.get("channel_code"));
        retValue.put("location", jsonObject.get("location"));
        retValue.put("locationCategory", jsonObject.get("loc_id"));
        retValue.put("freeType", jsonObject.get("freeType"));
        retValue.put("type", 0);//默认为零
        return retValue;
    }

    public static int analyzeResponseJson(JSONObject jsonObject){
        int retValue = 0;
        if (jsonObject == null) {
            retValue = ANALYZE_RESULT_ERROR;
        }else if (jsonObject.getIntValue("code") == ANALYZE_RESULT_SUC) {
            retValue = ANALYZE_RESULT_SUC;
        }
        return retValue;
    }

    public static int analyzeQueryJson(JSONObject jsonObject) {
        int retValue = 0;
        if (jsonObject == null) {
            retValue = ANALYZE_RESULT_ERROR;
        } else if (jsonObject.getIntValue("code") != ANALYZE_RESULT_SUC) {
            retValue = ANALYZE_RESULT_RESPONSE_FAIL;
        } else {
            try {
                JSONArray jsonArray = jsonObject.getJSONObject("response").getJSONArray("list");
                if (jsonArray.size() > 0) {
                    retValue = ANALYZE_RESULT_SUC;
                } else {
                    retValue = ANALYZE_RESULT_NO_DATA;
                }
            } catch (Exception e) {
                logger.error("analyzeQueryJson error", e);
                retValue = ANALYZE_RESULT_DECODE_ERROR;
            }
        }
        logger.info("analyzeQueryJson retValue {}", retValue);
        return retValue;
    }

    public static JSONObject queryChannelById(int channelId) {
        JSONObject jsonObject = ApiHttpRequestUtil.getMethod(VOD_SERVER_URL + "v1/channel?channel_id=" + channelId, null, null);
        return jsonObject;
    }

    public static String queryChannelByIdUrl(int channelId) {
        return VOD_SERVER_URL + "v1/channel?channel_id=" + channelId;
    }
}
